package org.example.lotterysystem.controller.handler;


import org.example.lotterysystem.common.errorcode.GlobalErrorCodeConstants;
import org.example.lotterysystem.common.exception.ControllerException;
import org.example.lotterysystem.common.exception.ServiceException;
import org.example.lotterysystem.common.pojo.CommonResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class GlobalExceptionHandler {
    private final  static Logger logger= LoggerFactory.getLogger(GlobalExceptionHandler.class);
    @ExceptionHandler(value = ServiceException.class)
    public CommonResult<?> serviceException(ServiceException e){

        //打印日志
        logger.error("ServiceException:",e);
        //构造返回结果
        return CommonResult.error(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode(), e.getMessage());
    }
    @ExceptionHandler(value = ControllerException.class)
    public CommonResult<?> ControllerException(ControllerException e){
        //打印日志
        logger.error("ControllerException:",e);

        //构造返回结果xiang
        return CommonResult.error(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode(),e.getMessage());
    }
    @ExceptionHandler(Exception.class)
    public CommonResult<?> exception(Exception e){
        logger.error("Exception:",e);
        return CommonResult.error(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode(), e.getMessage());
    }
}
